﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using AutoMapper;
using System.Data;

namespace Data
{
   public class NhaXuatBanRepository
    {
       private static NhaXuatBanDTO MapNhaXuatBanEFToNhaXuatBanDTO(NHAXUATBAN nhaxuatbanEF)
       {
           Mapper.CreateMap<NHAXUATBAN, NhaXuatBanDTO>();
           return Mapper.Map<NHAXUATBAN, NhaXuatBanDTO>(nhaxuatbanEF);
       }
       private static NHAXUATBAN MapNhaXuatBanDTOToNhaXuatBanEF(NhaXuatBanDTO nhaxuatbanDTO)
       {
           Mapper.CreateMap<NhaXuatBanDTO, NHAXUATBAN>();
           return Mapper.Map<NhaXuatBanDTO, NHAXUATBAN>(nhaxuatbanDTO);
       }
       public static List<NhaXuatBanDTO> GetAllNhaXuatBan()
       {
           List<NhaXuatBanDTO> list = new List<NhaXuatBanDTO>();
           try
           {
               using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
               {
                   var query = from nhaxuatban in context.NHAXUATBANList
                               select nhaxuatban;
                   foreach (var nhaxuatbanEF in query)
                   {
                       NhaXuatBanDTO nhaxuatbanDTO = MapNhaXuatBanEFToNhaXuatBanDTO(nhaxuatbanEF);
                       list.Add(nhaxuatbanDTO);
                   }
               }
           }
           catch (Exception)
           {

           }
           return list;

       }
       public static void Insert(NhaXuatBanDTO nhaxuatbanDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               NHAXUATBAN nhaxuatbanEF = new NHAXUATBAN();
               nhaxuatbanEF = MapNhaXuatBanDTOToNhaXuatBanEF(nhaxuatbanDTO);
               context.NHAXUATBANList.AddObject(nhaxuatbanEF);
               try
               {
                   context.SaveChanges();
               }
               catch (Exception)
               {
               }
              
           }
       }
       public static void Update(NhaXuatBanDTO nhaxuatbanDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               EntityKey key = context.CreateEntityKey("NHAXUATBANList", new NHAXUATBAN { MaNhaXuatBan = nhaxuatbanDTO.MaNhaXuatBan });
               NHAXUATBAN orignhaxuatban = context.GetObjectByKey(key) as NHAXUATBAN;
               context.Detach(orignhaxuatban);
               context.Attach(orignhaxuatban);
               NHAXUATBAN updatenhaxuatban = MapNhaXuatBanDTOToNhaXuatBanEF(nhaxuatbanDTO);
               context.ApplyCurrentValues<NHAXUATBAN>("NHAXUATBANList", updatenhaxuatban);
               try
               {
                   context.SaveChanges();
               }
               catch(Exception)
               {
               }

           }
       }
       public static bool Delete(int manhaxuatban)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   NHAXUATBAN orignhaxuatban =( from nhaxuatban in context.NHAXUATBANList
                                                  where nhaxuatban.MaNhaXuatBan == manhaxuatban
                                                  select nhaxuatban).First();
                   context.DeleteObject(orignhaxuatban);
                   context.SaveChanges();

               }
               catch
               {
                   return false;
               }
           }
           return true;
       }

    }
}
